#!/usr/bin/env bash
RED=$(printf '\033[31m')
PURPLE=$(printf '\033[0;35m')
GREEN=$(printf '\033[32m')
YELLOW=$(printf '\033[33m')
BLUE=$(printf '\033[34m')
BOLD=$(printf '\033[1m')
RESET=$(printf '\033[m')
printf "%s\n" "${GREEN}Starting ${YELLOW}x11vnc server${RESET} ..."
stopvnc -non-stop-dbus 2>/dev/null
export PULSE_SERVER=127.0.0.1
export DISPLAY=:233
TMOE_X11_PASSWD_FILE=${HOME}/.vnc/x11passwd
TMOE_X11_RESOLUTION=1440x720
TCP_PORT_FOR_RFB_PROTOCOL=5901
X11VNC_CURSOR_ARROW=2
TMOE_LINUX_DIR='/usr/local/etc/tmoe-linux'
TMOE_LOCALE_FILE="${TMOE_LINUX_DIR}/locale.txt"
TMOE_GIT_DIR="${TMOE_LINUX_DIR}/git"
TMOE_TOOL_DIR="${TMOE_GIT_DIR}/tools"
XSESSION_FILE='/etc/X11/xinit/Xsession'
TIGERVNC_CONFIG_FILE='/etc/tigervnc/vncserver-config-defaults'
#################
start_windows_10_pulse_audio_server() {
    . ${TMOE_LINUX_DIR}/wsl_pulse_audio
}
#####################
start_tmoe_x11vnc() {
    [[ ! -e ${TIGERVNC_CONFIG_FILE} ]] || . ${TIGERVNC_CONFIG_FILE} 2>/dev/null
    set -- "${@}" "-ncache_cr"
    set -- "${@}" "-xkb"
    set -- "${@}" "-noxrecord"
    #set -- "${@}" "-noxfixes"
    set -- "${@}" "-noxdamage"
    set -- "${@}" "-display" "${DISPLAY}"
    set -- "${@}" "-forever"
    set -- "${@}" "-bg"
    set -- "${@}" "-rfbauth" "${TMOE_X11_PASSWD_FILE}"
    set -- "${@}" "-users" "$(whoami)"
    set -- "${@}" "-rfbport" "${TCP_PORT_FOR_RFB_PROTOCOL}"
    set -- "${@}" "-noshm"
    set -- "${@}" "-desktop" "${desktop}"
    set -- "${@}" "-shared"
    set -- "${@}" "-verbose"
    set -- "${@}" "-cursor" "arrow"
    set -- "${@}" "-arrow" "${X11VNC_CURSOR_ARROW}"
    set -- "${@}" "-nothreads"
    set -- "x11vnc" "${@}"
    "${@}" &
}
###################
tmoe_x11vnc_preconfigure() {
    [[ -s "${TMOE_X11_PASSWD_FILE}" ]] || x11vncpasswd
    if [ -e "${TMOE_LOCALE_FILE}" ]; then
        TMOE_LANG=$(head -n 1 ${TMOE_LOCALE_FILE})
    else
        TMOE_LANG="zh_CN.UTF-8"
    fi
    export LANG="${TMOE_LANG}"
}
#################
source_tmoe_dbus_daemon() {
    . ${TMOE_TOOL_DIR}/gui/launch_dbus_daemon
}
###########
start_tmoe_xvfb() {
    set -- "${@}" "${DISPLAY}"
    set -- "${@}" "-screen" "0" "${TMOE_X11_RESOLUTION}x24"
    set -- "${@}" "-ac"
    set -- "${@}" "+extension" "GLX"
    set -- "${@}" "+render"
    set -- "${@}" "-deferglyphs" "16"
    set -- "${@}" "-br"
    set -- "${@}" "-wm"
    set -- "${@}" "-retro"
    set -- "${@}" "-noreset"
    set -- "Xvfb" "${@}"
    "${@}" &
}
####################
if [ "$(uname -r | cut -d '-' -f 3 | head -n 1)" = "Microsoft" ] || [ "$(uname -r | cut -d '-' -f 2 | head -n 1)" = "microsoft" ]; then
    start_windows_10_pulse_audio_server
fi
###############
start_x_session() {
    . ${XSESSION_FILE} &
}
#############
tmoe_x11vnc_preconfigure
source_tmoe_dbus_daemon
start_tmoe_xvfb
start_x_session
start_tmoe_x11vnc
###########
sleep 2s
TMOE_IP_ADDR=$(ip -4 -br -c a | awk '{print $NF}' | cut -d '/' -f 1 | grep -v '127\.0\.0\.1' | sed "s@\$@:${TCP_PORT_FOR_RFB_PROTOCOL}@")
case ${TMOE_LANG} in
zh_*UTF-8)
    cat <<-ENFOFX11VNCTIPS
正在启动x11vnc服务,本机默认vnc地址localhost:${TCP_PORT_FOR_RFB_PROTOCOL}
本机IPv6 vnc地址[::]:5900或::
The LAN VNC address 局域网地址${TMOE_IP_ADDR}
您可能会经历长达10多秒的黑屏
You may experience a black screen for up to 10 seconds.
您之后可以输startx11vnc启动，输stopvnc停止
You can type startx11vnc to start x11vnc,type stopvnc to stop it.
ENFOFX11VNCTIPS
    ;;
*)
    cat <<-EOF
Staring x11vnc server...
The ${YELLOW}local${RESET} x11vnc address is ${BLUE}localhost${GREEN}:${TCP_PORT_FOR_RFB_PROTOCOL}${RESET}
The ${YELLOW}LAN${RESET} x11vnc address is ${TMOE_IP_ADDR}
You may experience a black screen for up to 10 seconds.
You can type startx11vnc to start x11vnc,type stopvnc to stop it.
EOF
    ;;
esac
